﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace POS
{
    public partial class MenuItemsButtons : DevExpress.XtraEditors.XtraUserControl
    {
        List<Control> ButtonsMenuItem = new List<Control>(); // 1 thru 56
        List<Control> ButtonsMenuCategory = new List<Control>(); // 57 thru 88

        string ModificationMessage;
        string LastItem;

        bool Ismodification = false;

        double Total = OrderEntryScreen.CalculatedSubTotal;

        string LastCategory;

        public MenuItemsButtons(string categorys)
        {
            InitializeComponent();

            ButtonsMenuItem.Add(btnItem1); ButtonsMenuItem.Add(btnItem2);
            ButtonsMenuItem.Add(btnItem3); ButtonsMenuItem.Add(btnItem4);
            ButtonsMenuItem.Add(btnItem5); ButtonsMenuItem.Add(btnItem6);
            ButtonsMenuItem.Add(btnItem7); ButtonsMenuItem.Add(btnItem8);
            ButtonsMenuItem.Add(btnItem9); ButtonsMenuItem.Add(btnItem10);
            ButtonsMenuItem.Add(btnItem11); ButtonsMenuItem.Add(btnItem12);
            ButtonsMenuItem.Add(btnItem13); ButtonsMenuItem.Add(btnItem14);
            ButtonsMenuItem.Add(btnItem15); ButtonsMenuItem.Add(btnItem16);
            ButtonsMenuItem.Add(btnItem17); ButtonsMenuItem.Add(btnItem18);
            ButtonsMenuItem.Add(btnItem19); ButtonsMenuItem.Add(btnItem20);
            ButtonsMenuItem.Add(btnItem21); ButtonsMenuItem.Add(btnItem22);
            ButtonsMenuItem.Add(btnItem23); ButtonsMenuItem.Add(btnItem24);
            ButtonsMenuItem.Add(btnItem25); ButtonsMenuItem.Add(btnItem26);
            ButtonsMenuItem.Add(btnItem27); ButtonsMenuItem.Add(btnItem28);
            ButtonsMenuItem.Add(btnItem29); ButtonsMenuItem.Add(btnItem30);
            ButtonsMenuItem.Add(btnItem31); ButtonsMenuItem.Add(btnItem32);
            ButtonsMenuItem.Add(btnItem33); ButtonsMenuItem.Add(btnItem34);
            ButtonsMenuItem.Add(btnItem35); ButtonsMenuItem.Add(btnItem36);
            ButtonsMenuItem.Add(btnItem37); ButtonsMenuItem.Add(btnItem38);
            ButtonsMenuItem.Add(btnItem39); ButtonsMenuItem.Add(btnItem40);
            ButtonsMenuItem.Add(btnItem41); ButtonsMenuItem.Add(btnItem42);
            ButtonsMenuItem.Add(btnItem43); ButtonsMenuItem.Add(btnItem44);
            ButtonsMenuItem.Add(btnItem45); ButtonsMenuItem.Add(btnItem46);
            ButtonsMenuItem.Add(btnItem47); ButtonsMenuItem.Add(btnItem48);
            ButtonsMenuItem.Add(btnItem49); ButtonsMenuItem.Add(btnItem50);
            ButtonsMenuItem.Add(btnItem51); ButtonsMenuItem.Add(btnItem52);
            ButtonsMenuItem.Add(btnItem53); ButtonsMenuItem.Add(btnItem54);
            ButtonsMenuItem.Add(btnItem55); ButtonsMenuItem.Add(btnItem56);

            ButtonsMenuCategory.Add(btnItem57); ButtonsMenuCategory.Add(btnItem58);
            ButtonsMenuCategory.Add(btnItem59); ButtonsMenuCategory.Add(btnItem60);
            ButtonsMenuCategory.Add(btnItem61); ButtonsMenuCategory.Add(btnItem62);
            ButtonsMenuCategory.Add(btnItem63); ButtonsMenuCategory.Add(btnItem64);
            ButtonsMenuCategory.Add(btnItem65); ButtonsMenuCategory.Add(btnItem66);
            ButtonsMenuCategory.Add(btnItem67); ButtonsMenuCategory.Add(btnItem68);
            ButtonsMenuCategory.Add(btnItem69); ButtonsMenuCategory.Add(btnItem70);
            ButtonsMenuCategory.Add(btnItem71); ButtonsMenuCategory.Add(btnItem72);
            ButtonsMenuCategory.Add(btnItem73); ButtonsMenuCategory.Add(btnItem74);
            ButtonsMenuCategory.Add(btnItem75); ButtonsMenuCategory.Add(btnItem76);
            ButtonsMenuCategory.Add(btnItem77); ButtonsMenuCategory.Add(btnItem78);
            ButtonsMenuCategory.Add(btnItem79); ButtonsMenuCategory.Add(btnItem80);
            ButtonsMenuCategory.Add(btnItem81); ButtonsMenuCategory.Add(btnItem82);
            ButtonsMenuCategory.Add(btnItem83); ButtonsMenuCategory.Add(btnItem84);
            ButtonsMenuCategory.Add(btnItem85); ButtonsMenuCategory.Add(btnItem86);
            ButtonsMenuCategory.Add(btnItem87); ButtonsMenuCategory.Add(btnItem88);

            foreach (Control item in ButtonsMenuItem)
            {
                item.Text = "";
                item.MouseClick += new MouseEventHandler(btn_MouseClick);
            }

            foreach (Control item in ButtonsMenuCategory)
            {
                item.Text = "";
                item.MouseClick += new MouseEventHandler(btnCategorySelect_MouseClick);
            }


            int x = 0;
            foreach (KeyValuePair<string, string> category1 in DataTableClass.dictionaryCategorys)
            {
                if (category1.Key == categorys)
                {
                    //MessageBox.Show(category1.Key);
                    ButtonsMenuItem[x].Text = category1.Value.ToString();
                    x++;
                }
            }
        }

        private void btn_MouseClick(object sender, MouseEventArgs e)
        {
            var button = (SimpleButton)sender;

            ListViewItem lvi = new ListViewItem("1");
            lvi.SubItems.Add(button.Text.Trim());
            LastItem = button.Text.Trim();

            if (DataTableClass.dictionaryPrices.ContainsKey(button.Text))
            {
                double value = DataTableClass.dictionaryPrices[button.Text];
                lvi.SubItems.Add(value.ToString("C"));
                Total += double.Parse(value.ToString());
            }

            OrderEntryScreen._Main.listView1.Items.Add(lvi);

            OrderEntryScreen._Main.lblSubTotal.Text = Total.ToString("C");

        }

        private void btnCategorySelect_MouseClick(object sender, MouseEventArgs e)
        {
            var button = (SimpleButton)sender;
            LoadItems(button.Text.Trim());
            LastCategory = button.Text.Trim();
            LoadCategorys();
        }

        private void LoadCategorys()
        {
            int w = 0;
            foreach (string name in DataTableClass.categoryList)
            {
                ButtonsMenuCategory[w].Text = name;
                w++;
            }
        }

        private void LoadItems(string category)
        {
            foreach (SimpleButton button in ButtonsMenuItem)
            {
                button.Text = "";
            }


            int x = 0;
            foreach (KeyValuePair<string, string> category1 in DataTableClass.dictionaryCategorys)
            {
                if (category1.Value == category)
                {
                    //MessageBox.Show(category1.Key);
                    ButtonsMenuItem[x].Text = category1.Key.ToString();
                    x++;
                }
            }
        }

        private void MenuItemsButtons_Load(object sender, EventArgs e)
        {
            LoadCategorys();
            LoadItems("DRINK");
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            try
            {
                var SelectedItem = OrderEntryScreen._Main.listView1.SelectedItems[0];

                if (SelectedItem.ForeColor != Color.Green)
                {
                    string selectedAmount = SelectedItem.SubItems[2].Text;
                    string[] selectedAmountModifyed = selectedAmount.Split('$');

                    double LineAmount = Convert.ToDouble(selectedAmountModifyed[1]);

                    Total -= LineAmount;
                    OrderEntryScreen._Main.lblSubTotal.Text = Total.ToString("C");

                    SelectedItem.Remove();
                }
                else
                {
                    MessageBox.Show("Sorry you cannot delete an item already sent to Printer");
                }
                
            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message);
            }
        }
    }
}
